﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Resources;

namespace System.Windows.Media.Imaging
{
	public static class ExtensionMethods
	{
		/// <summary>
		/// Loads an image-resource from an assembly.
		/// </summary>
		/// <param name="bitmap"></param>
		/// <param name="AssemblyName">Name of the assembly that contains the resource.</param>
		/// <param name="ResourcePath">The path of the resource.</param>
		public static void LoadResource(this BitmapImage bitmap, string AssemblyName, string ResourcePath)
		{
			Uri uri = new Uri(AssemblyName + ";component/" + ResourcePath, UriKind.Relative);
			StreamResourceInfo sr = Application.GetResourceStream(uri);
			bitmap.SetSource(sr.Stream);
		}

		/// <summary>
		/// Loads an image-resource from an application-package.
		/// </summary>
		/// <param name="bitmap"></param>
		/// <param name="ResourcePath">The path of the resource</param>
		public static void LoadResource(this BitmapImage bitmap, string ResourcePath)
		{
			Uri uri = new Uri(ResourcePath, UriKind.Relative);
			StreamResourceInfo sr = Application.GetResourceStream(uri);
			bitmap.SetSource(sr.Stream);
		}

		static int GetPixelIndex(this WriteableBitmap wb, int x, int y, int width)
		{
			int index = x + y * width;
			if (index >= wb.Pixels.Length)
				return -1;
			return index;
		}

		public static int GetPixelIndex(this WriteableBitmap wb, int x, int y)
		{
			return wb.GetPixelIndex(x, y, wb.PixelWidth);
		}

		static int GetPixel(this WriteableBitmap wb, int x, int y, int width)
		{
			int index = wb.GetPixelIndex(x, y, width);
			if (index < 0)
				return -1;
			return wb.Pixels[index];
		}

		public static int GetPixel(this WriteableBitmap wb, int x, int y)
		{
			return wb.GetPixel(x, y, wb.PixelWidth);
		}

		static void SetPixel(this WriteableBitmap wb, int x, int y, int width, int value)
		{
			int index = wb.GetPixelIndex(x, y, width);
			if (index < 0)
				return;
			wb.Pixels[index] = value;
		}

		public static void SetPixel(this WriteableBitmap wb, int x, int y, int value)
		{
			wb.SetPixel(x, y, wb.PixelWidth, value);
		}

		public static WriteableBitmap Crop(this BitmapSource image, Rect cropArea)
		{
			WriteableBitmap source = new WriteableBitmap(image);
			int xOff = (int)cropArea.X;
			int yOff = (int)cropArea.Y;
			int width = (int)cropArea.Width;
			int height = (int)cropArea.Height;
			WriteableBitmap dest = new WriteableBitmap(width, height);
			int sourceWidth = source.PixelWidth;
			for (int x = 0; x < width; x++)
			{
				for (int y = 0; y < height; y++)
				{
					dest.SetPixel(x, y, width, source.GetPixel(x + xOff, y + yOff, sourceWidth));
				}
			}
			dest.Invalidate();
			return dest;
		}

		public static WriteableBitmap MakeTransparent(this BitmapSource image, Color TransparencyKey)
		{
			WriteableBitmap wb = new WriteableBitmap(image);
			int c = TransparencyKey.ToInt();
			int trans = Colors.Transparent.ToInt();
			for (int i = 0; i < wb.Pixels.Length; i++)
			{
				if (wb.Pixels[i] == c)
					wb.Pixels[i] = trans;
			}
			wb.Invalidate();
			return wb;
		}
	}
}
